package com.sm.controller;


import com.sm.model.DynamicCollect;
import com.sm.service.DynamicCollectService;
import com.sm.service.DynamicCommentService;
import com.sm.util.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 动态的分享和收藏
 */
@Controller
@RequestMapping("/dynamicCollect")
public class DynamicCollectController {
    @Autowired
    private DynamicCollectService dynamicCollectService;
    @Autowired
    private DynamicCommentService dynamicCommentService;

    /**
     * 将收藏记录添加到数据库
     * @param dynamicCollect
     * @return
     */
    @RequestMapping("/addDynamicCollect")
    @ResponseBody
    public Msg addDynamicCollect(DynamicCollect dynamicCollect) {

        dynamicCollectService.addDynamicCollect(dynamicCollect);
        return Msg.success();
    }

    /**
     * 根据用户ID拉取此用户所有收藏记录
     * @param collectUser
     * @return
     */
    @RequestMapping("/selectDynamicCollectById")
    @ResponseBody
    public Msg CommentCollect(@RequestParam("collectUser") Integer collectUser) {

        List<DynamicCollect> collects = dynamicCollectService.collectComment(collectUser);

        if (ObjectUtils.isEmpty(collects)) {
            return Msg.success().add("collects", collects);
        }
        return Msg.fail();
    }

    /**
     * 取消收藏接口：将这条收藏记录删除
     * @param collectUser
     * @return
     */
    @RequestMapping("/delectCollectByDynamic")
    @ResponseBody
    public Msg deleteCollect(@RequestParam("collectUser") Integer collectUser) {

        if (collectUser == null) {
            return Msg.fail().add("msg", "参数不为空！");
        }
        dynamicCollectService.deleteCollect(collectUser);
        return Msg.success().add("msg", "删除成功");
    }

    /**
     *查询评论人数
     * @param dynamicId
     * @return
     */
    @RequestMapping("/selectUserNumber")
    @ResponseBody
    public Msg selectUserNumber(@RequestParam("dynamicId")Integer dynamicId) {
        int i = dynamicCommentService.selectUserNumber(dynamicId);
        if(i<0){
        return Msg.fail();
    }
        return Msg.success().add("i",i);
    }
}
